package main

import (
	"fmt"
	"github.com/biocrosscoder/flex/typed/collections"
	"github.com/biocrosscoder/flex/typed/collections/arraylist"
	"github.com/biocrosscoder/flex/typed/collections/dict"
)

func isValid(brackets string) (valid bool) {
	stack := collections.NewStack[string](len(brackets))
	leftBrackets := arraylist.Of("(", "[", "{")
	leftBracketsOfRightBrackets := dict.Dict[string, string]{
		")": "(",
		"]": "[",
		"}": "{",
	}
	for _, r := range brackets {
		bracket := string(r)
		if leftBrackets.Includes(bracket) {
			stack.Push(bracket)
			continue
		}
		partner, ok := stack.Pop()
		if !ok || partner != leftBracketsOfRightBrackets[bracket] {
			return
		}
	}
	valid = stack.Empty()
	return
}

func main() {
	bracketStrings := []string{
		"()",
		"()[]{}",
		"(]",
		"([])",
	}
	for _, brackets := range bracketStrings {
		info := "valid"
		if !isValid(brackets) {
			info = "in" + info
		}
		fmt.Printf("%s is %s\n", brackets, info)
	}
}
